﻿using System;
using System.Linq;
using Biz.EF.Platform;
using Hjt212Parser;
using log4net;


namespace TcpServer.DataProcessor
{
    class Platform2031Processor 
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(Platform2031Processor));

     
        public static void Persist(DustContext context, string msg)
        {
            var mn = Parser.GetStringByKey("MN", msg);

            if (string.IsNullOrWhiteSpace(mn)) return;
            try
            {
                //有效字符串
                if (Parser.IsLengthRight(msg))
                {
                    //根据MN号获取基本信息
                    var equipment = BaseInfoBufferAndHelper.GetBaseWholeInfoByMn(mn);
                    //有效企业信息
                    if (equipment != null)
                    {
                        var datatime = Parser.GetDateTimeByKey("DataTime", msg);
                        //从字符串获取污染物代码s
                        var pollutantCodes = Parser.GetPollutantCodes(msg);
                        if (datatime.HasValue)
                        {

                            var main =
                                context.DayDataRCMains.FirstOrDefault(t => t.MonitorTime.Year == datatime.Value.Year && t.MonitorTime.Month == datatime.Value.Month && t.MonitorTime.Day == datatime.Value.Day);
                            if (main == null)
                            {
                                main = new DayDataRCMain()
                                {
                                    StatCodeID = equipment.StatCodeID,
                                    DevCodeID = equipment.DevCodeID,
                                    MonitorTime = datatime.Value
                                };
                                context.DayDataRCMains.Add(main);
                            }
                            foreach (var code in pollutantCodes)
                            {
                                var factor = BaseInfoBufferAndHelper.GetFactorByCode(code);
                                if (factor == null) continue;
                                var detail = new DayDataRCDetail();
                                detail.MonitorFactorID = factor.ID;
                                //var flag = Parser.GetStringByKeyFromContent(code + "-Flag", content.ReceivedMsg);
                                //if (flag.Equals("N"))
                                //{
                                    detail.Avg = Parser.GetDoubleByKey(code + "-Avg", msg);
                                    detail.Min = Parser.GetDoubleByKey(code + "-Min", msg);
                                    detail.Max = Parser.GetDoubleByKey(code + "-Max", msg);
                                //}
                                main.DayDataRCDetails.Add(detail);
                            }
                            context.SaveChanges();
                        }
                    }
                    else
                    {
                        log.Warn(mn + " 没有对应基础信息！");
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error( ex);
                log.Error(msg);

            }
        }
      
    }
}
